Read a File in Python
To programmatically read data from a file using Python, it must be opened first. Use the built-in open()
function:
file_object = open(file_name [, access_mode][, buffering])
Here are the parameter details:
file_name
: The name of the file you want to access.access_mode
: Determines the mode in which the file is opened, e.g., read, write, append (optional, default is read mode).buffering
: Specifies buffering options (optional).
These two statements are identical:
fo = open("foo.txt", "r")
fo = open("foo.txt")
Reading Text Filesβ
To read data from the opened file, use the read()
method of the File object:
file_object.read([count])
Here, count
is the number of bytes to be read from the file (optional). If count
is missing, it reads until the end of the file.
count β Number of bytes to be read.
Exampleβ
# Open a file
fo = open("foo.txt", "r")
text = fo.read()
print(text)
# Close the opened file
fo.close()
Reading Binary Filesβ
To handle files like media or executables, use binary mode by adding a 'b' prefix:
f = open('test.bin', 'wb')
data = b"Hello World"
f.write(data)
f.close()
Exampleβ
f = open('test.bin', 'rb')
data = f.read()
print(data.decode(encoding='utf-8'))
Reading Numbers from a Fileβ
Integer Dataβ
To write and read integer data in a binary file:
n = 25
n_bytes = n.to_bytes(8, 'big')
f = open('test.bin', 'wb')
f.write(n_bytes)
To read back from the file:
f = open('test.bin', 'rb')
data = f.read()
n = int.from_bytes(data, 'big')
print(n)
Float Dataβ
For floating point data, use the struct
module:
import struct
x = 23.50
data = struct.pack('f', x)
f = open('test.bin', 'wb')
f.write(data)
Exampleβ
f = open('test.bin', 'rb')
data = f.read()
x = struct.unpack('f', data)
print(x)
Reading Using Reading and Writing (r+) Modeβ
To perform both read and write operations simultaneously:
fo = open("foo.txt", "r+")
fo.seek(10, 0)
data = fo.read(3)
print(data)
fo.close()
The File object also supports the seek() function to rewind the stream to read from any desired byte position.
Following is the syntax for seek() method β
fileObject.seek(offset[, whence])
Parametersβ
-
offset β This is the position of the read/write pointer within the file.
-
whence β This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.
Simultaneous Read and Writeβ
For simultaneous read and write, use 'r+' or 'w+' mode:
fo = open("foo.txt", "r+")
fo.write("This is a rat race")
fo.seek(10, 0)
data = fo.read(3)
fo.seek(10, 0)
fo.write('cat')
fo.close()
Read a File from Specific Offsetβ
The seek()
method sets the file's current position. Example:
Following is the syntax for seek() method β
fileObject.seek(offset[, whence])
Parametersβ
-
offset β This is the position of the read/write pointer within the file.
-
whence β This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file's end.
Examplesβ
fo = open("foo.txt", "r+")
fo.seek(10, 0)
data = fo.read()
print(data)
fo.close()
This program reads from a specific offset and prints the data.
This is a cat race